import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        toolBar =QToolBar()

        cb = QCheckBox('IsReadOnly')
        cb.stateChanged.connect(self.OnIsReadOnlyChanged)
        toolBar.addWidget(cb)        

        cb = QCheckBox('Italic')
        cb.stateChanged.connect(self.OnItalicChanged)
        toolBar.addWidget(cb)

        cb = QCheckBox('Underline')
        cb.stateChanged.connect(self.OnUnderlineChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Alignment:'))
        alignment = QComboBox()
        alignment.addItems(Qt.AlignmentFlag.__members__.keys())
        alignment.currentTextChanged.connect(self.AlignmentChanged)
        toolBar.addWidget(alignment)

        toolBar.addWidget(QLabel('LineWrapMode:'))
        lineWrapMode = QComboBox()        
        lineWrapMode.addItems(QTextEdit.LineWrapMode.__members__.keys())
        lineWrapMode.currentTextChanged.connect(self.LineWrapModeChanged)
        toolBar.addWidget(lineWrapMode)

        toolBar.addWidget(QLabel('AutoFormatting:'))
        AutoFormatting = QComboBox()        
        AutoFormatting.addItems(QTextEdit.AutoFormattingFlag.__members__.keys())
        AutoFormatting.currentTextChanged.connect(self.AutoFormattingChanged)
        toolBar.addWidget(AutoFormatting)        

        layout.addWidget(toolBar)       
        
        toolBar2=QToolBar()  

        toolBar2.addWidget(QLabel('WordWrapMode:'))
        wordWrapMode = QComboBox()        
        wordWrapMode.addItems(QTextOption.WrapMode.__members__.keys())
        wordWrapMode.currentTextChanged.connect(self.WordWrapModeChanged)
        toolBar2.addWidget(wordWrapMode)

        toolBar2.addWidget(QLabel('PlaceholderText:'))
        self.placeholder = QLineEdit()
        self.placeholder.editingFinished.connect(self.SetPlaceholderText)
        toolBar2.addWidget(self.placeholder)

        toolBar2.addWidget(QLabel('Font:'))
        font = QFontComboBox()
        font.currentFontChanged.connect(self.FontChanged)
        toolBar2.addWidget(font)        

        btn = QPushButton('ShowHtml')
        btn.clicked.connect(self.ShowHtml)
        toolBar2.addWidget(btn)

        btn = QPushButton('ShowText')
        btn.clicked.connect(self.ShowText)
        toolBar2.addWidget(btn)
        
        layout.addWidget(toolBar2)

        self.textEdit=QTextEdit()
        layout.addWidget(self.textEdit)

        self.setLayout(layout) 
    def OnIsReadOnlyChanged(self,state):
        self.textEdit.setReadOnly(state)
        self.parent.OnShowMessage(f"Set ReadOnly={state}")
    def OnItalicChanged(self,state):
        self.textEdit.setFontItalic(state)
        self.parent.OnShowMessage(f"Set FontItalic={state}")
    def OnUnderlineChanged(self,state):
        self.textEdit.setFontUnderline(state)
        self.parent.OnShowMessage(f"Set FontUnderline={state}")
    def AlignmentChanged(self,text):
        alignment = Qt.AlignmentFlag[text]
        self.textEdit.setAlignment(alignment)
        self.parent.OnShowMessage(f'Set Alignment: {alignment}')
    def LineWrapModeChanged(self,text):
        lineWrapMode=QTextEdit.LineWrapMode[text]
        self.textEdit.setLineWrapMode(lineWrapMode)
        self.parent.OnShowMessage(f'Set LineWrapMode: {lineWrapMode}')
    def AutoFormattingChanged(self,text):
        autoFormatting=QTextEdit.AutoFormattingFlag[text]
        self.textEdit.setAutoFormatting(autoFormatting)
        self.parent.OnShowMessage(f'Set AutoFormatting: {autoFormatting}')
    def WordWrapModeChanged(self,text):
        wordWrapMode=QTextOption.WrapMode[text]
        self.textEdit.setWordWrapMode(wordWrapMode)
        self.parent.OnShowMessage(f'Set WordWrapMode: {wordWrapMode}')
    def SetPlaceholderText(self):
        placeholder = self.placeholder.text()
        self.textEdit.setPlaceholderText(placeholder)
        self.parent.OnShowMessage(f'Set PlaceholderText: {placeholder}')
    def FontChanged(self,font):
        family = font.family()
        self.textEdit.setFontFamily(family)
        self.parent.OnShowMessage(f'Set FontFamily: {family}')
    def ShowHtml(self):
        self.parent.OnShowMessage(f'html: {self.textEdit.toHtml()}')
    def ShowText(self):
        self.parent.OnShowMessage(f'text: {self.textEdit.toPlainText()}')

def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet